﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Power.Common;
using System.Windows.Forms;
using Interfaces;

namespace Power.Manager
{
    public static class Manager
    {
        #region entity
        private static IMeter _meterEntity;
        public static IMeter MeterEntity
        {
            get { return Manager._meterEntity; }
            set { Manager._meterEntity = value; }
        }
        #endregion
        public static void BuildSystem()
        {
            try
            {
                //load config
                string file = Application.StartupPath + "\\Config\\SystemConfig.xml";                
                Config.SystemConfig.Instance.LoadConfig(file);
                //get db connect
                //DBUtility.PubConstant.GetConnectionString("ConnectionString");
                DBUtility.PubConstant.SetConnString(Config.SystemConfig.Instance.GetConnString());
                //get run mode
                SystemStatus.Instance.RunType = (RunMode)Enum.Parse(typeof(RunMode), ConfigurationManager.AppSettings.Get("RunMode"));
                //imeter 
                string mType = ConfigurationManager.AppSettings.Get("MeterType");
                switch (mType)
                {
                    case "PH630":                        
                        _meterEntity = MeterControl.MeterPH600.Instance;
                        break;
                    case "EQ600":
                        _meterEntity = MeterControl.MeterEQ600.Instance;
                        break;
                    case "CL400":
                        break;
                    default:
                        //data base read
                        _meterEntity = MeterControl.MeterSimulator.Instance;
                        break;

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void Initalize()
        {
            //start scan port
            Manager._meterEntity.GetCommPort();
            //Manager._meterEntity.OpenPort();
            Manager._meterEntity.Start();            
        }
        public static void ExitSystem()
        {
            Manager._meterEntity.ClosePort();
        }

        #region interface

        #endregion
    }
}
